// pages/supply/supply.js
var common = require("../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        color_header: "#fff",
        color_text: "white",
        color_main: "",
        color_minor: "",
        info: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getBase();
        this.getInfo();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor
                });
            }
        });
    },
    formSubmit: function(e) {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        if (e.detail.value.title == "") {
            wx.showModal({
                title: "提示",
                content: "供应商名称不能为空"
            });
            return false;
        }
        if (e.detail.value.phone == "") {
            wx.showModal({
                title: "提示",
                content: "手机号码不能为空"
            });
            return false;
        }
        var reg2 = /^[1][0-9][0-9]{9}$/;
        if (!reg2.test(e.detail.value.phone)) {
            wx.showModal({
                title: "提示",
                content: "请填写正确手机号"
            });
            return false;
        }
        if (e.detail.value.linkman == "") {
            wx.showModal({
                title: "提示",
                content: "供应商联系人不能为空"
            });
            return false;
        }
        common.request({
            endpoint: "My/supplier/supplier_apply",
            method: "post",
            data: {
                openid: openid,
                info: e.detail.value
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                wx.showToast({
                    title: "提交成功",
                    icon: "success",
                    success: res => {
                        setTimeout(() => {
                            wx.switchTab({
                                url: "/pages/personal/personal"
                            });
                        }, 500);
                    }
                });
            },
            fail: res => {
                wx.showModal({
                    title: "提示",
                    content: "申请失败",
                    success: res => {
                        setTimeout(() => {
                            wx.switchTab({
                                url: "/pages/personal/personal"
                            });
                        }, 500);
                    }
                });
            }
        });
    },
    getInfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/supplier/supplier_read",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra
                });
            }
        });
    }
});